//
//  PInformationViewController.m
//  HouseShow
//
//  Created by gongjie on 11-12-26.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "PInformationViewController.h"
#import "TitleTableViewContrller.h"
#import "CGUISplitViewController.h"
#import "SubViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CJSONDeserializer.h"

@implementation PInformationViewController

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
-(id)init{
    if (self = [super init]) {
        pagenum = 1;
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    _pinfoview = [[CGUISplitViewController alloc] init];
    [_pinfoview.view setBackgroundColor:[UIColor whiteColor]];
    _pinfoview.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
    
    _titleview = [[TitleTableViewContrller alloc] initWithStyle:UITableViewStylePlain];
    _titleview.delegate = self;
//  [_titleview.tableView setBackgroundColor:[UIColor yellowColor]];
    UINavigationController* tirlena = [[UINavigationController alloc] initWithRootViewController:_titleview];
    _titleview.title = @"第1页";
    tirlena.navigationBar.barStyle = UIBarStyleBlack;
    tirlena.view.layer.borderWidth =1.f;
    tirlena.view.layer.borderColor =[UIColor grayColor].CGColor;
//    [_titleview release];

    _showview = [[SubViewController alloc] init];
    _showview.delegate = self;
    UINavigationController* shownaviget = [[UINavigationController alloc] initWithRootViewController:_showview];
    shownaviget.navigationBar.barStyle = UIBarStyleBlack;
//  [_showview release];
    
    _pinfoview.viewControllers  = [NSArray arrayWithObjects:tirlena, shownaviget, nil];
     _pinfoview.delegate = _titleview;
    [self.view addSubview:_pinfoview.view];
    
    LoginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,320,self.view.frame.size.height)];
    [LoginView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    Login = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    Login.frame = CGRectMake(LoginView.frame.size.width/2-25, LoginView.frame.size.height/2-130, 50, 50);
    [LoginView addSubview:Login];
    
    NSUInteger shuzi = pagenum;
    [self requestpage:shuzi+1 End:pagenum*20];
    [Login startAnimating];
    [self.view addSubview:LoginView];
}
-(void)changelist
{
    
}
#pragma mark SubDelegate
-(void)Revocation
{
    [self dismissModalViewControllerAnimated:YES];
}
#pragma mark TitleTableVieDelegate
-(void)getindex:(NSUInteger)index
{
    NSDictionary* dictionary =(NSDictionary*)[_titleview.Listdate objectAtIndex:index];
    if (dictionary) {
        NSString* url = [dictionary objectForKey:@"url"];
        if (url !=nil&&url.length>0) {
            [_showview startlist:url];  
        }
    }
   
}


-(void)SendPreviousPage
{
    if (pagenum>1) {pagenum--;}

    NSUInteger shuzi = pagenum;
    [self requestpage:shuzi*pagenum+1 End:pagenum*20];
    [Login startAnimating];
    [self.view addSubview:LoginView];

}
-(void)SendNextPage
{
    pagenum++;
    NSUInteger shuzi = pagenum;
    [self requestpage:shuzi*pagenum+1 End:pagenum*20];
    [Login startAnimating];
    [self.view addSubview:LoginView];
    
}
-(void)requestpage:(NSUInteger)begin End:(NSUInteger)end;
{
    NSString* str = @"http://www.unitrue.com.cn:8088/ClientWebPage/CNewsList.aspx?tid=20";
    NSString* url = [[NSString alloc] initWithFormat:@"%@&begin=%d&end=%d",str,begin,end];
    NSLog(@"sss%@",url);
    NSURL* tReqURL = [NSURL URLWithString:url];
    ASIHTTPRequest* tLMRequest = [ASIHTTPRequest requestWithURL:tReqURL];
    [tLMRequest setDelegate:self];
    [tLMRequest setDidFailSelector:@selector(requestpageFailure:)];
    [tLMRequest setDidFinishSelector:@selector(requestpageSuccess:)];
    [tLMRequest startAsynchronous];
}
-(void)requestpageSuccess:(ASIHTTPRequest*)request
{
    NSString* httpdata = [request responseString];
	NSLog(@"http context = %@",httpdata);
	NSData *jsonData = [httpdata dataUsingEncoding:NSUTF32BigEndianStringEncoding];
	NSError *error = nil;
	NSDictionary* tDataDic = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] ;
	if (error == nil) {
        NSMutableArray* data = [NSMutableArray arrayWithArray:[tDataDic objectForKey:@"20"]];
        if ([data count]>0){
            [_titleview.Listdate removeAllObjects];
            for (NSDictionary* object in data) {
                [_titleview.Listdate addObject:object];
            }
            if (LoginView.superview) {
                [LoginView removeFromSuperview];
            }
            [_titleview.tableView reloadData];
        }
        else
        {
            if (LoginView.superview) {
                [LoginView removeFromSuperview];
            }
            if (pagenum>1) {
                pagenum--;
            }
            if ( [_titleview.Listdate count]) {
                UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下一页无数据" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                tAlertView.tag =1002;
                [tAlertView show];
                [tAlertView release];
            }else{
                UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"没获得数据" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
                tAlertView.tag =1001;
                [tAlertView show];
                [tAlertView release];
            }
  
        }
       
        NSString* str = [NSString stringWithFormat:@"第%d页",pagenum];
        _titleview.title = str;
    }
}
-(void)requestpageFailure:(ASIHTTPRequest*)request
{
    UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败，请检查网络后重新运行！" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
    tAlertView.tag =1000;
    [tAlertView show];
    [tAlertView release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
#pragma mark UISplitViewControllerDelegate
// Called when a button should be added to a toolbar for a hidden view controller
- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc
{
    
}

// Called when the view is shown again in the split view, invalidating the button and popover controller
- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    
}

// Called when the view controller is shown in a popover so the delegate can take action like hiding other popovers.
- (void)splitViewController: (UISplitViewController*)svc popoverController: (UIPopoverController*)pc willPresentViewController:(UIViewController *)aViewController
{
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1001) {
        [self dismissModalViewControllerAnimated:YES];
    }
}
-(void)dealloc
{
    [_pinfoview release];
    [_titleview release];
    [_showview release];
    [LoginView release];
    [Login release];
    [super dealloc];
}

@end
